package com.igeek.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.constant.MessageConstant;
import com.igeek.collateral.constant.RedisImgConstant;
import com.igeek.collateral.entity.Role;
import com.igeek.collateral.entity.User;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IRoleService;
import com.igeek.collateral.service.IUserService;
import com.igeek.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/userRole")
@Api(tags = "用户接口")
@Slf4j
public class UserRoleController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("findPage")
    @ApiOperation("条件分页查询用户信息")
    public Result findPage(PageQueryVO vo) {
        try {
            Page<User> page = userService.findPage(vo);
            if (page != null) {
                return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, page);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }


    @GetMapping("{id}")
    @ApiOperation("根据id查询用户信息")
    public Result findOne(@PathVariable("id") Integer id) {
        User user = userService.getById(id);
        if(user != null){
            return new Result(true, "查询用户成功", user);
        }
         return new Result(false, "查询用户失败");
    }
    @GetMapping("findR")
    @ApiOperation("查询所有角色信息")
    public Result findAllRole() {
            List<Role> list = roleService.list();
            if (!CollectionUtils.isEmpty(list)) {
                return new Result(true, "查询所有角色成功", list);
            }
        return new Result(false, "查询所有角色失败");
    }

    @GetMapping("findRoleByUserId/{id}")
    @ApiOperation("根据用户id查询角色信息")
    public Result findRoleByUserId(@PathVariable("id")  Integer userId) {
        List<Integer> roleIds = userService.findRoleByUserId(userId);
        if (!CollectionUtils.isEmpty(roleIds)) {
            return new Result(true,"通过用户id查询到角色信息成功" , roleIds);
        }
        return new Result(false,"通过用户id查询到角色信息失败");
    }
    @PutMapping("updateUser")
    @ApiOperation("更新用户信息")
    public Result updateUser(@RequestBody User user, @RequestParam("roleIds") Integer[] roleIds) {

        boolean flag = userService.updateUser(user, roleIds);
        if (flag) {
            return new Result(true, "更新用户信息成功");
        }
        return new Result(false, "更新用户信息失败");
    }

    @GetMapping("findAll")
    @ApiOperation("查询所有角色")
    public Result findAll() {
        try {
            List<Role> list =roleService.list();
            if (!CollectionUtils.isEmpty(list)) {
                return new Result(true, "获取角色成功", list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"获取角色失败");
    }

    @PostMapping("add")
    @ApiOperation("添加用户以及角色信息")
    public Result add(@RequestBody User user, @RequestParam("roleIds") Integer[] roleIds) {
        boolean flag = userService.add(user, roleIds);
        if (flag) {
            redisTemplate.opsForSet().add(RedisImgConstant.UPLOAD_DB_IMG ,user.getPic());
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
    }



    //图片上传
    @PostMapping("upload")
    @ApiOperation("角色图片上传")
    public Result upload(MultipartFile imgFile) throws IOException {
        //返回的是图片名字，拼接上七牛云的域名进行显示
        String pic = userService.uploadByQiniuyun(imgFile);
        if (StringUtils.hasLength(pic)) {
            redisTemplate.opsForSet().add(RedisImgConstant.UPLOAD_IMG , pic);

            return new Result(true,"上传图片成功", (Object) pic);
        }
        return new Result(false, "上传图片失败");
    }
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除用户信息")
    public Result deleteById(@PathVariable("id") Integer roleId) {
        boolean flag = userService.del(roleId);
        if (flag) {
            return new Result(true, "删除成功");
        }
        return new Result(false,"删除失败");
    }

}


